<?php
// +----------------------------------------------------------------------
// | Description: ordersItems
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-03-15 10:48:48
// +----------------------------------------------------------------------

namespace app\client\model;

use app\common\model\CommonClient;

class OrdersItem extends CommonClient
{
    protected $fuzzyConditions = null;
    protected $clearConditions = ['OrderId'];
    protected $returnFields = '';
    protected $rangeConditions = null;
    protected $union_fuzzyConditions = null;
    protected $foreignConditions = [
        'shop_orders' => ['Id', 'OrderId']
    ];
    protected $name = 'shop_orders_items';

    public function getCountList()
    {
        $ProductName = isset($this->param['ProductName']) ? $this->param['ProductName'] : '';
        $ProductCode = isset($this->param['ProductCode']) ? $this->param['ProductCode'] : '';
        $Content = isset($this->param['Content']) ? $this->param['Content'] : '';
        $where = '';
        if(isset($this->param['ProductName'])){
            if($where == ''){
                $where = "WHERE p.ProductName LIKE '%$ProductName%'";
            }
        }
        if(isset($this->param['ProductCode'])){
            if($where == ''){
                $where = "WHERE p.ProductCode LIKE '%$ProductCode%'";
            }else{
                $where .= " AND p.ProductCode LIKE '%$ProductCode%'";
            }
        }
        if(isset($this->param['Content'])){
            if($where == ''){
                $where = "WHERE s.Content LIKE '%$Content%'";
            }else{
                $where .= " AND s.Content LIKE '%$Content%'";
            }
        }

        if(isset($this->param['ShippingDate'])){
            $date = explode('--', $this->param['ShippingDate']);
            $StartDate = $date[0];
            $EndDate = $date[1];
            $sql = "SELECT temp.Quantity,temp.OrderCount, p.ProductName, p.ProductCode, s.Content FROM (SELECT
                shop_orders_items.SkuId,
                sum(shop_orders_items.Quantity) AS Quantity,
                count(DISTINCT shop_orders.Id) AS OrderCount
            FROM
                shop_orders_items
            INNER JOIN shop_orders ON shop_orders.Id = shop_orders_items.OrderId
            WHERE
                OrderStatus > 1
            AND ShippingDate >= '$StartDate'
            AND ShippingDate < '$EndDate'
            GROUP BY
                shop_orders_items.SkuId) as temp
            INNER JOIN shop_goods_sku as s ON temp.SkuId = s.Id
            INNER JOIN shop_goods_products as p ON p.Id = s.ProductId
            $where";
        }else{
            $sql = "SELECT temp.Quantity,temp.OrderCount, p.ProductName, p.ProductCode, s.Content FROM (SELECT
                shop_orders_items.SkuId,
                sum(shop_orders_items.Quantity) AS Quantity,
                count(DISTINCT shop_orders.Id) AS OrderCount
            FROM
                shop_orders_items
            INNER JOIN shop_orders ON shop_orders.Id = shop_orders_items.OrderId
            WHERE
                OrderStatus > 2
            GROUP BY
                shop_orders_items.SkuId) as temp
            INNER JOIN shop_goods_sku as s ON temp.SkuId = s.Id
            INNER JOIN shop_goods_products as p ON p.Id = s.ProductId
            $where";
        }
        $data = $this->query($sql);
        $res['list'] = $data;
        $res['dataCount'] = count($data);
        return $res;
    }

    public function filter($data)
    {
        foreach($data['list'] as $k => $v)
        {
            $data['list'][$k]['OrderId'] = (string)$v['OrderId'];       
        }
        return $data;
    }
}
